package org.fanrui.hrm.Enum;

import lombok.Getter;
import org.springframework.util.StringUtils;

@Getter
public enum ViewContentType {
    DEFAULT("default", "application/octet-stream"),
    JPG("jpg", "image/jpeg"),
    TIFF("tiff", "image/tiff"),
    GIF("gif", "image/gif"),
    JFIF("jfif", "image/jpeg"),
    PNG("png", "image/png"),
    TIF("tif", "image/tiff"),
    ICO("ico", "image/x-icon"),
    JPEG("jpeg", "image/jpeg"),
    WBMP("wbmp", "image/vnd.wap.wbmp"),
    FAX("fax", "image/fax"),
    NET("net", "image/pnetvue"),
    JPE("jpe", "image/jpeg"),
    TXT("txt", "text/plain"),
    XLX("xls", "application/vnd.ms-excel"),
    XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
    DOC("doc", "application/msword"),
    DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
    RP("rp", "image/vnd.rn-realpix");

    private String prefix;

    private String type;

    /**
     * 根据文件名前缀获取内容类型
     *
     * @param prefix 文件名前缀，通常是文件扩展名
     * @return 对应的内容类型字符串
     */
    public static String getContentType(String prefix) {
        // 如果前缀为空或空白，则返回默认内容类型
        if (StringUtils.isEmpty(prefix)) {
            return DEFAULT.getType();
        }
        // 提取文件扩展名作为前缀
        prefix = prefix.substring(prefix.lastIndexOf(".") + 1);
        // 遍历所有已定义的内容类型
        for (ViewContentType value : ViewContentType.values()) {
            // 忽略大小写比较前缀，如果匹配则返回对应的内容类型
            if (prefix.equalsIgnoreCase(value.getPrefix())) {
                return value.getType();
            }
        }
        // 如果没有匹配的内容类型，则返回默认内容类型
        return DEFAULT.getType();
    }


    ViewContentType(String prefix, String type) {
        this.prefix = prefix;
        this.type = type;
    }

}
